/**
 * @param {string} s
 * @return {number}
 */
var lengthOfLongestSubstring = function (s) {
    var sArr = Array.from(s);
    let res = 0;
    for (let i = 0; i < sArr.length; i++) {
        var map = {};
        let num = 0;
        for (let j = i; j < sArr.length; j++) {

            //重复了就直接返回 或者 已经存在过的直接返回
            if ((sArr[i] === sArr[j] && i !== j) || map[sArr[j]] !== undefined) {
                break
            }
            ++num;
            map[sArr[j]] = 1;
            if (num > res) {
                res = num;
            }
        }
    }
    return res;
};
console.log(lengthOfLongestSubstring("abca"));

var lengthOfLongestSubstring = function (s) {
    let res = 0;
    const map = {};
    let start = 0;
    for (let end = 0; end < s.length; end++) {
        var alpha = s.charAt(end);
        if (map[alpha] !== undefined) {
            start = Math.max(map[alpha], start);
        }
        res = Math.max(res, end - start + 1);
        map[s.charAt(end)] = end + 1;
    }
    return res;
};

var lengthOfLongestSubstring = function (s) {
    // 哈希集合，记录每个字符是否出现过
    const occ = new Set();
    const n = s.length;
    // 右指针，初始值为 -1，相当于我们在字符串的左边界的左侧，还没有开始移动
    let rk = -1, ans = 0;
    for (let i = 0; i < n; ++i) {
        if (i != 0) {
            // 左指针向右移动一格，移除一个字符
            occ.delete(s.charAt(i - 1));
        }
        while (rk + 1 < n && !occ.has(s.charAt(rk + 1))) {
            // 不断地移动右指针
            occ.add(s.charAt(rk + 1));
            ++rk;
        }
        // 第 i 到 rk 个字符是一个极长的无重复字符子串
        ans = Math.max(ans, rk - i + 1);
    }
    return ans;
};

var lengthOfLongestSubstring = function (s) {
    if (s.length === 0) {
        return 0;
    }
    var bitSet = {};
    let res =0, left =0 , right = 0;
    while (left < s.length) {
        var rs = s.charAt(right);
        var ls = s.charAt(left);
        if (bitSet[rs]) {
            bitSet[ls] = false;
            left++;
        } else {
            bitSet[rs] = true;
            right++;
        }
        //计算长度
        if (res < (right - left)) {
            res = right - left;
        }

        if(left + res >= s.length || right >= s.length){
            break;
        }

    }
    return res;
};

console.log(lengthOfLongestSubstring("pwekpww"));